<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/Frog_32px_1177822_easyicon.net.ico">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/Frog_32px_1177822_easyicon.net.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/Frog_16px_1177822_easyicon.net.ico">
  <link rel="mask-icon" href="/images/Frog_32px_1177822_easyicon.net.ico" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="/lib/pace/pace-theme-minimal.min.css">
  <script src="/lib/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"hxy1997.xyz","root":"/","scheme":"Pisces","version":"7.8.0","exturl":false,"sidebar":{"position":"left","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":"valine","storage":true,"lazyload":true,"nav":null,"activeClass":"valine"},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"输入关键字","hits_empty":"没有找到与「${query}」相关搜索","hits_stats":"${hits} 条相关记录，共耗时 ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.json"};
  </script>

  <meta name="description" content="By Viral Shah | Nov 26, 2018 转自原文+翻译 js一门很容易入门但是很难精通的语言。我很认同这句话。这是因为js是一门古老的语言同时也是一门很灵活的语言。有着一堆神秘的语法和过时的功能。我已经使用js很多年了，迄今为止，我时不时地会发现一些我从未知道的隐藏语法或技巧。 我试图列出一些鲜为人知的js特性。虽然有一些特性在严格模式下是不能用的，但是它依然是一种不错的js特性">
<meta property="og:type" content="article">
<meta property="og:title" content="鲜为人知的JavaScript功能">
<meta property="og:url" content="https://hxy1997.xyz/2021/04/20/%E9%B2%9C%E4%B8%BA%E4%BA%BA%E7%9F%A5%E7%9A%84JavaScript%E5%8A%9F%E8%83%BD/index.html">
<meta property="og:site_name" content="hxy的博客">
<meta property="og:description" content="By Viral Shah | Nov 26, 2018 转自原文+翻译 js一门很容易入门但是很难精通的语言。我很认同这句话。这是因为js是一门古老的语言同时也是一门很灵活的语言。有着一堆神秘的语法和过时的功能。我已经使用js很多年了，迄今为止，我时不时地会发现一些我从未知道的隐藏语法或技巧。 我试图列出一些鲜为人知的js特性。虽然有一些特性在严格模式下是不能用的，但是它依然是一种不错的js特性">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210724172728.webp">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210420095442.webp">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210420095532.webp">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210420095604.webp">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210420095701.webp">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210420095729.webp">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210420100233.webp">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210420100256.webp">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210420100634.webp">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210420100659.webp">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210420100735.webp">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210420101418.webp">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210420102334.webp">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210420102007.webp">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210420102059.webp">
<meta property="article:published_time" content="2021-04-20T04:00:00.000Z">
<meta property="article:modified_time" content="2021-07-24T09:32:59.959Z">
<meta property="article:author" content="hxy">
<meta property="article:tag" content="javascript">
<meta property="article:tag" content="功能特性">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210724172728.webp">

<link rel="canonical" href="https://hxy1997.xyz/2021/04/20/%E9%B2%9C%E4%B8%BA%E4%BA%BA%E7%9F%A5%E7%9A%84JavaScript%E5%8A%9F%E8%83%BD/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>鲜为人知的JavaScript功能 | hxy的博客</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="hxy的博客" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">hxy的博客</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">Mia san Mia!</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

  <a href="https://github.com/huxingyi1997" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://hxy1997.xyz/2021/04/20/%E9%B2%9C%E4%B8%BA%E4%BA%BA%E7%9F%A5%E7%9A%84JavaScript%E5%8A%9F%E8%83%BD/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/Robben.gif">
      <meta itemprop="name" content="hxy">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="hxy的博客">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          鲜为人知的JavaScript功能
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-04-20 12:00:00" itemprop="dateCreated datePublished" datetime="2021-04-20T12:00:00+08:00">2021-04-20</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-07-24 17:32:59" itemprop="dateModified" datetime="2021-07-24T17:32:59+08:00">2021-07-24</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/web%E5%89%8D%E7%AB%AF/" itemprop="url" rel="index"><span itemprop="name">web前端</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="热度" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">热度：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2021/04/20/%E9%B2%9C%E4%B8%BA%E4%BA%BA%E7%9F%A5%E7%9A%84JavaScript%E5%8A%9F%E8%83%BD/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2021/04/20/%E9%B2%9C%E4%B8%BA%E4%BA%BA%E7%9F%A5%E7%9A%84JavaScript%E5%8A%9F%E8%83%BD/" itemprop="commentCount"></span>
    </a>
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>By <a target="_blank" rel="noopener" href="https://blog.usejournal.com/@viral_shah">Viral Shah</a> | Nov 26, 2018 转自<a target="_blank" rel="noopener" href="https://blog.usejournal.com/little-known-features-of-javascript-901665291387">原文</a>+<a target="_blank" rel="noopener" href="https://juejin.cn/post/6844903740688826381">翻译</a></p>
<p>js一门很容易入门但是很难精通的语言。我很认同这句话。这是因为js是一门古老的语言同时也是一门很灵活的语言。有着一堆神秘的语法和过时的功能。我已经使用js很多年了，迄今为止，我时不时地会发现一些我从未知道的隐藏语法或技巧。</p>
<p>我试图列出一些鲜为人知的js特性。虽然有一些特性在严格模式下是不能用的，但是它依然是一种不错的js特性。然而，请注意，我不建议你全使用上我介绍的特性。虽然它们很酷，但毕竟是鲜为人知的特性，你的同事可能会看不懂。</p>
<span id="more"></span>

<h2 id="1-void-操作符"><a href="#1-void-操作符" class="headerlink" title="1.void 操作符"></a>1.void 操作符</h2><p>js有一元运算符。你可能已经见过了，像 void(0) 或者 void0 。它只有一个目的 - 在右边评估表达式并返回undefined。使用0只是一种惯例。不一定要使用0.它可以是任何有效的表达式，如</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">void</span> &lt;expression&gt;</span><br></pre></td></tr></table></figure>

<p>它仍然返回undefined。</p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210724172728.webp" alt="img"></p>
<p>为什么要这样子返回undefined，直接返回undefined不好么？看起来这个特性很多余，不是么？</p>
<h3 id="有趣的事实"><a href="#有趣的事实" class="headerlink" title="有趣的事实"></a>有趣的事实</h3><p>好吧，事实证明，在ES5之前，你实际上可以在大多数浏览器中为undefined重新分配值。类似这样</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="literal">undefined</span> = “abc”</span><br></pre></td></tr></table></figure>

<p>使用void的话会始终保持返回正确的undefined。</p>
<h2 id="2-构造函数的括号是可选的"><a href="#2-构造函数的括号是可选的" class="headerlink" title="2.构造函数的括号是可选的"></a>2.构造函数的括号是可选的</h2><p>对的，你没看错，当调用构造函数的时候，我们在类名后面加的圆括号——完全是可选的！😮（前提是你不需要传递参数）</p>
<p>下面的代码样式都被认为是有效的JS语法，并且会给你完全相同的结果！</p>
<img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210420095442.webp" alt="img" style="zoom:50%;" />



<h2 id="3-IIFE的括号可以被跳过"><a href="#3-IIFE的括号可以被跳过" class="headerlink" title="3.IIFE的括号可以被跳过"></a>3.IIFE的括号可以被跳过</h2><p>IIFE的语法让我感觉总是有点奇怪。</p>
<p>IIFE所有的括号都有什么用？</p>
<p>那些额外的括号只是为了告诉js解析器，即将到来的代码是函数表达式而不是函数。知道了这些，来想一想，有很多方法可以跳过这些额外的括号，仍然可以制作有效的IIFE。</p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210420095532.webp" alt="img"></p>
<p>void操作符告诉解析器这段代码是函数表达式。因此，我们可以省略包着函数的括号。你猜怎么着？我们可以使用任何一元运算符（void，+ ,!， - 等），这仍然有效。</p>
<p>真酷！</p>
<p>然而，如果你观察再仔细点，你会有个疑问，</p>
<p>一元运算符不会影响IIFE返回的结果吗？</p>
<p>好吧，它会影响结果。但是有个好消息，如果你关心返回的结果并且把结果放在某个变量中，那么你首先不需要额外的括号。</p>
<p>这就对了！</p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210420095604.webp" alt="img"></p>
<p>我们添加这些括号只是为了更好的代码可读性。</p>
<h2 id="4-with-声明"><a href="#4-with-声明" class="headerlink" title="4.with 声明"></a>4.with 声明</h2><p>你知道js有with声明块？with一直是js的关键词。语法如下：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">with</span> (object)</span><br><span class="line">   statement </span><br><span class="line"><span class="comment">// for multiple statements add a block</span></span><br><span class="line"><span class="keyword">with</span> (object) &#123;</span><br><span class="line">   statement</span><br><span class="line">   statement</span><br><span class="line">   ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>with可以对传递过来的对象进行解析，在with 块里可以直接使用对象的属性。</p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210420095701.webp" alt="img"></p>
<h3 id="有趣的事实-1"><a href="#有趣的事实-1" class="headerlink" title="有趣的事实"></a>有趣的事实</h3><p>with块看起来很酷对不对？甚至比object destructuring还好。</p>
<p>但是，并不是这样的。</p>
<p>通常不鼓励使用with语句，因为它已被弃用。 在严格模式下完全禁止。 事实证明，with块会增加语言中的一些性能和安全性问题。Bummer</p>
<h2 id="5-构造函数"><a href="#5-构造函数" class="headerlink" title="5.构造函数"></a>5.构造函数</h2><p>function语句并不是定义新函数的唯一方法；你可以使用【Function（）】这个构造函数和new运算符动态定义函数。</p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210420095729.webp" alt="img"></p>
<p>构造函数的最后一个参数是字符串化代码，就是你写的函数。其他参数是该函数的参数。</p>
<h3 id="有趣的事实-2"><a href="#有趣的事实-2" class="headerlink" title="有趣的事实"></a>有趣的事实</h3><p>在js，Function构造函数是所有构造函数的构造函数。也包括Object的构造函数。并且Function构造函数的构造函数就是它自身。因此，调用object.constructor.constructor…足够多次后，在js中，最终会在任何对象返回Function构造函数。</p>
<h2 id="6-Function-的属性"><a href="#6-Function-的属性" class="headerlink" title="6.Function 的属性"></a>6.Function 的属性</h2><p>我们知道在js中，Functions是首个对象。因此，没有人能阻止我们在Function对象上添加自己的属性。这是可以的，但是很少会这样做。</p>
<p>那有什么场景会应用到呢？</p>
<p>有一些很好的用例。例如，</p>
<p>让Functions可配置化</p>
<p>我们有个greet函数。我们想要让我们的函数在不同的环境下打印不同语言的问候语。这个环境是可以配置的。我们可以在某处维护个全局环境变量，或者使用上文所说的添加自定义属性</p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210420100233.webp" alt="img"></p>
<h3 id="Function的静态变量"><a href="#Function的静态变量" class="headerlink" title="Function的静态变量"></a>Function的静态变量</h3><p>另一个相似的例子。假设你想要实现一个生成一系列有序数字的数字生成器。通常来说，你会用Class或者IIFE，在里面维护一个计数变量，然后输出有序的值。这样我们就可以限制对计数器的访问，并避免使用额外的变量来污染全局空间。</p>
<p>但是，如果我们希望灵活地读取甚至修改计数器并且不污染全局空间呢？</p>
<p>好吧，我们仍然可以创建一个Class，带有一个计数器变量和一些额外的方法来读取它; 或者我们可以懒一点，只需在函数上使用属性。</p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210420100256.webp" alt="img"></p>
<h2 id="7-Arguments属性"><a href="#7-Arguments属性" class="headerlink" title="7.Arguments属性"></a>7.Arguments属性</h2><p>我相信大多数人都知道函数中的参数对象。 它是一个像对象一样的数组（译者：emmm），在所有函数中都能访问到。它具有在调用函数时传递的参数列表，也有其他一些有趣的属性。</p>
<ul>
<li>arguments.callee：指向当前调用函数</li>
<li>arguments.callee.caller：指向当前被调函数的调用者。</li>
</ul>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210420100634.webp" alt="img"></p>
<p>注意：虽然ES5禁止在严格模式下使用callee和caller，但在许多编译库中仍然常见。 所以，值得学习它们。</p>
<h2 id="8-标记模版字面量"><a href="#8-标记模版字面量" class="headerlink" title="8.标记模版字面量"></a>8.标记模版字面量</h2><p>除非你是井底之蛙，你一定听说过模版字面量。模版字面量时es6的一个很酷的补充。然而，你听说过Tagged模版字面量么（Tagged Template Literals）？</p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210420100659.webp" alt="img"></p>
<p>Tagged模板字面量允许您通过向模板字面量添加自定义标记来更好地控制将模板字面量解析为字符串。Tagged只是一个获取字符串模板解析后的所有字符串和值的数组的解析器函数。Tagged函数最终返回字符串。</p>
<p>在下面的示例中，我们的自定义标记 - highlight，解释模板文字的值，并使用</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;mark&gt;</span><br></pre></td></tr></table></figure>

<p>元素将解释的值包装在结果字符串中，以突出显示。</p>
<h2 id="9-Getter-amp-Setter"><a href="#9-Getter-amp-Setter" class="headerlink" title="9.Getter &amp; Setter"></a>9.Getter &amp; Setter</h2><p>在大多数情况下，js对象时很简单的。比如说有一个对象叫user，我们尝试使用user.age来读取age属性时，如果有定义并赋值了，就有返回。如果没有，就返回undefined。简单。</p>
<p>但是，它不一定非常简单。 JavaScript对象具有Getters和Setter的概念。 我们可以编写自定义的Getter函数来返回我们想要的任何内容，而不是直接返回对象上的值。 设置值也是一样的。</p>
<p>这使我们可以在获取或设置属性时可以灵活的创造虚拟属性、验证属性。</p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210420100735.webp" alt="img"></p>
<p>Getters和Setters不是es5的特性，它们是一直都存在的特性。es5只是添加了语法糖。</p>
<h2 id="10-逗号操作符"><a href="#10-逗号操作符" class="headerlink" title="10.逗号操作符"></a>10.逗号操作符</h2><p>JavaScript有一个逗号运算符。 它允许我们在一行中编写由逗号分隔的多个表达式，并返回最后一个表达式的结果</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// syntax</span></span><br><span class="line"><span class="keyword">let</span> result = expression1, expression2,... expressionN</span><br></pre></td></tr></table></figure>

<p>来看看上面的例子，这里将会解析所有的表达式，并把result变量赋予expressionN返回的值。</p>
<p>你可能在循环里面使用过了</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> (<span class="keyword">var</span> a = <span class="number">0</span>, b = <span class="number">10</span>; a &lt;= <span class="number">10</span>; a++, b--)</span><br></pre></td></tr></table></figure>

<p>它可以帮助我们把语句写在一行</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getNextValue</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> counter++, <span class="built_in">console</span>.log(counter), counter</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>或者写lamda表达式</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> getSquare = <span class="function"><span class="params">x</span> =&gt;</span> (<span class="built_in">console</span>.log (x), x * x)</span><br></pre></td></tr></table></figure>



<h2 id="11-加操作符"><a href="#11-加操作符" class="headerlink" title="11.+ 加操作符"></a>11.+ 加操作符</h2><p>想要知道如何便捷地把字符串转换成number？</p>
<p>只要在字符串前面加个+号。</p>
<p>Plus运算符也适用于负，八进制，十六进制，指数值。 更重要的是，它甚至可以将Date或Moment.js对象转换为时间戳！</p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210420101418.webp" alt="img"></p>
<h2 id="12-Bang-Bang-操作符"><a href="#12-Bang-Bang-操作符" class="headerlink" title="12.!! Bang Bang 操作符"></a>12.!! Bang Bang 操作符</h2><p>好的，严格来讲，它不是一个单独的JavaScript运算符。 它只是两次使用的JavaScript否定运算符。</p>
<p>但是Bang Bang听起来很酷！Bang Bang是一个可以讲任何表达式转换成布尔值的方法。</p>
<p>如果表达式是一个true，则返回true；否则返回false。</p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210420102334.webp" alt="img"></p>
<h2 id="13-位运算作符"><a href="#13-位运算作符" class="headerlink" title="13.~ 位运算作符"></a>13.~ 位运算作符</h2><p>来看一个没人关注的位运算操作符。我们什么时候用它呢？</p>
<p>好吧，有个日常用例。</p>
<p>当与数字一起使用时，Tilde运算符有效，像这样</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">~<span class="function"><span class="params">N</span> =&gt;</span> -（N + <span class="number">1</span>）。</span><br></pre></td></tr></table></figure>

<p>仅当N == -1时，此表达式的计算结果为“0”</p>
<p>我们可以通过在indexOf（…）函数前面放置〜来进行布尔检查是否有一项存在。String或Array中都可以用这个。</p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210420102007.webp" alt="img"></p>
<p>注意：ES6和ES7分别在String＆Array中添加了一个新的.includes（）方法。当然，它比位运算运算符更清晰，以检查项目是否存在于Array或String中。</p>
<h2 id="14-标签语句"><a href="#14-标签语句" class="headerlink" title="14.标签语句"></a>14.标签语句</h2><p>js具有标签语句的概念。它允许我们在js中命名循环和块。然后，我们可以使用这些标签在使用break或continue时使用。</p>
<p>带标签的语句在嵌套循环中特别方便。但是我们也可以使用它们来简单地将代码组织成块或创建一个可退出的块</p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210420102059.webp" alt="img"></p>
<p>注意：不像其他的语言，js没有goto语句。因此，我们只能使用labels来搭配break和continue使用。</p>

    </div>

    
    
    
      
<div>
        <div style="text-align:center;color: #ccc;font-size:14px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
</div>
        

  <div class="followme">
    <p>欢迎关注我的其它发布渠道</p>

    <div class="social-list">

        <div class="social-item">
          <a target="_blank" class="social-link" href="/atom.xml">
            <span class="icon">
              <i class="fa fa-rss"></i>
            </span>

            <span class="label">RSS</span>
          </a>
        </div>
    </div>
  </div>


      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/javascript/" rel="tag"><i class="fa fa-tag"></i> javascript</a>
              <a href="/tags/%E5%8A%9F%E8%83%BD%E7%89%B9%E6%80%A7/" rel="tag"><i class="fa fa-tag"></i> 功能特性</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2021/04/16/49%E9%81%93Promise%E9%9D%A2%E8%AF%95%E9%A2%98/" rel="prev" title="49道Promise面试题">
      <i class="fa fa-chevron-left"></i> 49道Promise面试题
    </a></div>
      <div class="post-nav-item">
    <a href="/2021/04/21/%E5%88%A9%E7%94%A8Typora+PicGo+Github+Jsdelivr%E6%90%AD%E5%BB%BA%E5%B1%9E%E4%BA%8E%E8%87%AA%E5%B7%B1%E7%9A%84%E5%9B%BE%E5%BA%8A/" rel="next" title="利用Typora+PicGo+Github+Jsdelivr搭建属于自己的图床">
      利用Typora+PicGo+Github+Jsdelivr搭建属于自己的图床 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-void-%E6%93%8D%E4%BD%9C%E7%AC%A6"><span class="nav-text">1.void 操作符</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%9C%89%E8%B6%A3%E7%9A%84%E4%BA%8B%E5%AE%9E"><span class="nav-text">有趣的事实</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0%E7%9A%84%E6%8B%AC%E5%8F%B7%E6%98%AF%E5%8F%AF%E9%80%89%E7%9A%84"><span class="nav-text">2.构造函数的括号是可选的</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-IIFE%E7%9A%84%E6%8B%AC%E5%8F%B7%E5%8F%AF%E4%BB%A5%E8%A2%AB%E8%B7%B3%E8%BF%87"><span class="nav-text">3.IIFE的括号可以被跳过</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-with-%E5%A3%B0%E6%98%8E"><span class="nav-text">4.with 声明</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%9C%89%E8%B6%A3%E7%9A%84%E4%BA%8B%E5%AE%9E-1"><span class="nav-text">有趣的事实</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0"><span class="nav-text">5.构造函数</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%9C%89%E8%B6%A3%E7%9A%84%E4%BA%8B%E5%AE%9E-2"><span class="nav-text">有趣的事实</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-Function-%E7%9A%84%E5%B1%9E%E6%80%A7"><span class="nav-text">6.Function 的属性</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#Function%E7%9A%84%E9%9D%99%E6%80%81%E5%8F%98%E9%87%8F"><span class="nav-text">Function的静态变量</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#7-Arguments%E5%B1%9E%E6%80%A7"><span class="nav-text">7.Arguments属性</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#8-%E6%A0%87%E8%AE%B0%E6%A8%A1%E7%89%88%E5%AD%97%E9%9D%A2%E9%87%8F"><span class="nav-text">8.标记模版字面量</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#9-Getter-amp-Setter"><span class="nav-text">9.Getter &amp; Setter</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#10-%E9%80%97%E5%8F%B7%E6%93%8D%E4%BD%9C%E7%AC%A6"><span class="nav-text">10.逗号操作符</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#11-%E5%8A%A0%E6%93%8D%E4%BD%9C%E7%AC%A6"><span class="nav-text">11.+ 加操作符</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#12-Bang-Bang-%E6%93%8D%E4%BD%9C%E7%AC%A6"><span class="nav-text">12.!! Bang Bang 操作符</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#13-%E4%BD%8D%E8%BF%90%E7%AE%97%E4%BD%9C%E7%AC%A6"><span class="nav-text">13.~ 位运算作符</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#14-%E6%A0%87%E7%AD%BE%E8%AF%AD%E5%8F%A5"><span class="nav-text">14.标签语句</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="hxy"
      src="/images/Robben.gif">
  <p class="site-author-name" itemprop="name">hxy</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">80</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">8</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">120</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/huxingyi1997" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;huxingyi1997" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:huxingyi1997@zju.edu.cn" title="E-Mail → mailto:huxingyi1997@zju.edu.cn" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-frog"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">hxy</span>
</div>

<div class="theme-info">
  <div class="powered-by"></div>
  <span class="post-count">博客全站共1039.2k字</span>
</div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/lozad@1/dist/lozad.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="/js/local-search.js"></script>













  

  


<script>
NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//unpkg.com/valine/dist/Valine.min.js', () => {
    var GUEST = ['nick', 'mail', 'link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el         : '#valine-comments',
      verify     : false,
      notify     : true,
      appId      : 'pQsO3ySbU4VtWN2j1FLA74Ha-gzGzoHsz',
      appKey     : 'QYacMDY2VY7Wazprg1X6FiUv',
      placeholder: "Just go go",
      avatar     : 'mm',
      meta       : guest,
      pageSize   : '10' || 10,
      visitor    : false,
      lang       : 'zh-cn' || 'zh-cn',
      path       : location.pathname,
      recordIP   : false,
      serverURLs : ''
    });
  }, window.Valine);
});
</script>

  
  <!-- 动态背景特效 -->
  <!-- 樱花特效 -->
    <script async src="/js/src/sakura.js"></script>
    <script async src="/js/src/fairyDustCursor.js"></script>
</body>
</html>
